<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程名称" prop="flowName">
        <el-input
          v-model="queryParams.flowName"
          placeholder="请输入流程名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleAdd('teacher')"  size="mini"
        v-hasPermi="['labdanger:purchase:teacher']">老师申购</el-button>
        <el-button type="primary" @click="handleAdd('academy')"  size="mini"
        v-hasPermi="['labdanger:purchase:academy']">学校采购</el-button>
      </el-form-item>
      <PurchaseDialog ref="editDialog" @refresh="getList"></PurchaseDialog>
      <PurchasePreview ref="preview" @refresh="getList"></PurchasePreview>
    </el-form>
    <el-table v-loading="loading" :data="purchaseList" >
      <el-table-column label="编号"  prop="instanceId" align="center" width="60"/>
      <el-table-column label="流程名称"  prop="flowName" :show-overflow-tooltip="true"/>
      <el-table-column label="申请类别"  prop="applyOwnerOpt" align="center" width="60">
        <template slot-scope="scope">
          <span v-if="scope.row.applyOwnerOpt=='teacher'">老师申购</span>
          <span v-if="scope.row.applyOwnerOpt=='academy'">学院采购</span>
        </template>
      </el-table-column>
      <el-table-column label="创建用户"  prop="createBy" width="100px" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间"  prop="createTime"  width="140px" align="center">
      </el-table-column>
      <el-table-column label="提交时间" prop="startTime"  width="140px" align="center">
      </el-table-column>
      <el-table-column label="流程状态" align="center" prop="flowStatusOpt" :formatter="flowStatusOptFormat" width="60px"/>
      <el-table-column label="结束时间" align="center" prop="endTime" width="160px">
      </el-table-column>
      <el-table-column label="状态" align="center" prop="applyStatusOpt" :formatter="applyStatusOptFormat" width="60px"/>
      <el-table-column label="操作" align="center" width="80px">
        <template slot-scope="scope">
          <el-button v-if= "scope.row.flowStatusOpt=='uncommitted'"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['labdanger:purchase:edit']"
          >修改</el-button>
          <el-button v-if= "scope.row.flowStatusOpt=='uncommitted'"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['labdanger:purchase:edit']"
          >删除</el-button>
          <el-button v-if= "scope.row.flowStatusOpt!='uncommitted'"
            size="mini"
            type="text"
            @click="handlePreview(scope.row)"

          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listPurchase,getPurchase,delPurchase  } from "@/api/labdanger/purchase";
import PurchaseDialog from "@/views/labdanger/dangerComps/purchaseDialog.vue";
import PurchasePreview from "@/views/labdanger/dangerComps/purchasePreview.vue";
import ToDoTask from "@/views/labdata/flowTask/todo_task.vue"
export default {
  name: "MyCreate",
  components: {PurchaseDialog, PurchasePreview},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 申请采购表格数据
      purchaseList: [],
      // 申请状态字典
      applyStatusOptOptions: [],
     // 流程状态字典
     flowStatusOptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flowName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("applyStatusOpt").then(response => {
      this.applyStatusOptOptions = response.data;
    });
    this.getDicts("flowStatusOpt ").then(response => {
      this.flowStatusOptOptions = response.data;
    });
    this.loading = false
  },
  methods: {
   /** 查询申请采购列表 */
   getList() {
     this.loading = true;
     listPurchase(this.queryParams).then(response => {
       this.purchaseList = response.rows;
       this.total = response.total;
       this.loading = false;
     });
   },
   // 申请状态字典翻译
   applyStatusOptFormat(row, column) {
     return this.selectDictLabel(this.applyStatusOptOptions, row.applyStatusOpt);
   },
   // 流程状态字典翻译
   flowStatusOptFormat(row, column) {
     return this.selectDictLabel(this.flowStatusOptOptions, row.flowStatusOpt);
   },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        instanceId: null,
        createTime: null,
        createUserId: null,
        flowName: null,
        applyStatusOpt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd(busiType) {
      this.$refs["editDialog"].handleAdd(busiType);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs["editDialog"].handleUpdate(row);
    },
    handlePreview(row){
      this.$refs["preview"].handleUpdate(row);
    },
    handleDelete(row){
      let id = row.instanceId;
      this.$confirm('是否确认删除采购单编号为"' + id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPurchase(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.instanceId != null) {
            updatePurchase(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchase(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
